﻿using JncInterProcess;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VisionBaseLib
{
    public class GNVision
    {
        InterProcess m_process = null;
        public delegate void ShowVisionImagedelegate(int index, Bitmap img);
        public  ShowVisionImagedelegate _ShowVisionImagedelegate=null;

        public GNVision()
        {
        }
        public void Init()
        {
            //初始化
            m_process = new InterProcess();
            m_process.SetTimeout(50);                        //设置命令回复超时时间
            m_process.srcImageNotifier += onSrcImageGrabbed;         //实时图像回调
            m_process.resultImageNotifier += onResultImageGrabbed;   //结果图像回调
            m_process.resultDataRecvedNotifier += onResultDataRecved;

            m_process.pwStartNotifier += onPWStartRecved;
            m_process.pwEndNotifier += onPWEndRecved;
            m_process.pwErrorNotifier += onPWErrorRecved;


            m_process.SetVisionPath(@"C:\Program Files (x86)\HuaYaVisionKit_x64\HuaYaVisionKit_x64.exe");
            m_process.StartVision();
            m_process.Start();
            m_process.StartRun();
        }


        //实时图像回调
        void onSrcImageGrabbed(int index, Bitmap img)
        {
            if(_ShowVisionImagedelegate!=null)
              _ShowVisionImagedelegate(index, img);

        }

        //结果图像回调
        void onResultImageGrabbed(int index, Bitmap img)
        {
            if (_ShowVisionImagedelegate != null)
                _ShowVisionImagedelegate(index, img);
        }

        void onResultDataRecved(int screen_key, List<NgTool> ngToolList,
            List<JudgedValue> judgedValueList, List<MeasuredValue> measuredValueList)
        {
            int i = 0;
            ++i;
        }

        private void onPWStartRecved()
        {
            int i = 0;
            ++i;
        }

        private void onPWEndRecved()
        {
            int i = 0;
            ++i;
        }

        private void onPWErrorRecved()
        {
            int i = 0;
            ++i;
        }

        public void StartVision()
        {
            //启动程序
            bool rtn = m_process.StartVision();
        }
        public void ExitVision()
        {
            //退出程序运行
            bool rtn = m_process.ExitVision();
        }
        public  void StartRun()
        {
            //开始运行
            bool rtn = m_process.StartRun();
        }
        public  void ShowVisionWindow()
        {
            //显示窗口
            bool rtn = m_process.ShowVisionWindow();
        }
        public  void HideVisionWindow()
        {
            //隐藏窗口
            bool rtn = m_process.HideVisionWindow();
        }
        public  void GetPorgramList(ref List<ProgramInfo> info)
        {
            //获取工程列表
            bool rtn = m_process.GetPorgramList(out info);
        }
        public  void GetCurrentPorgram(out string current_program)
        {
            current_program = "";
        }
        public  bool SwitchProgram(string name)
        {

            return m_process.SwitchProgram(name);
        }
        public void Stop()
        {
            if(m_process != null)
            m_process.Stop();
        }
    }
}
